package com.robot.service;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.robot.constant.BuildTestStatus;
import com.robot.constant.RunMode;
import com.robot.dao.*;
import com.robot.model.*;
import com.robot.util.SessionUtil;
import com.robot.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/11/24
 * Time: 13:35
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Service
@Slf4j
public class BuildTestService {

    @Autowired
    TestCaseMapper testCaseMapper;
    @Autowired
    InterfaceMapper interfaceMapper;
    @Autowired
    BuildTestMapper buildTestMapper;
    @Autowired
    BuildTestInterfaceMapper buildTestInterfaceMapper;
    @Autowired
    BuildTestCaseMapper buildTestCaseMapper;
    @Autowired
    TestPlanService testPlanService;

    /**
     * 按照接口执行
     **/
    @Transactional
    public ResponseMessage runTestByInterface(Integer[] interfaceIds) {
        BuildTestDo buildTestDo = new BuildTestDo();
        buildTestDo.setTestPlanName("接口临时运行计划");
        buildTestDo.setStatus(BuildTestStatus.WATING);
        buildTestDo.setRunMode(RunMode.CASERUN);
        User user = SessionUtil.getSessionUser();
        buildTestDo.setUserName(user.getUserName());
        buildTestDo.setProjectId(user.getLastProjectId());
        buildTestMapper.addBuildTest(buildTestDo);
        Integer buildTestId = buildTestDo.getId();
        for (Integer interfaceId : interfaceIds) {
            Protocol protocol = interfaceMapper.getProtocolById(interfaceId);
            BuildTestInterfaceDo buildTestInterfaceDo = new BuildTestInterfaceDo();
            buildTestInterfaceDo.setBuildTestId(buildTestId);
            BeanUtils.copyProperties(protocol, buildTestInterfaceDo);
            buildTestInterfaceMapper.addBuildTestInterface(buildTestInterfaceDo);
            Integer buildTestInterfaceId = buildTestInterfaceDo.getId();

            TestCase testCase = new TestCase();
            testCase.setInterfaceId(interfaceId);
            List<TestCase> testCaseList = testCaseMapper.getTestCaseList(testCase);
            this.addBuildTestCase(testCaseList, buildTestInterfaceId);
        }
        return ResponseMessage.successResponse(buildTestId);
    }

    /**
     * 按照测试用例执行，这些用例一定是在某一个接口下面的
     **/
    @Transactional
    public ResponseMessage createBuildByTestCase(Integer[] caseIds) {
        BuildTestDo buildTestDo = new BuildTestDo();
        buildTestDo.setTestPlanName("临时运行的计划");
        buildTestDo.setStatus(BuildTestStatus.WATING);
        buildTestDo.setRunMode(RunMode.CASERUN);
        User user = SessionUtil.getSessionUser();
        buildTestDo.setUserName(user.getUserName());
        buildTestDo.setProjectId(user.getLastProjectId());

        buildTestMapper.addBuildTest(buildTestDo);
        Integer buildTestId = buildTestDo.getId();
        /**查询运行的测试用例**/
        List<TestCase> testCaseList = testCaseMapper.getTestCaseByIds(Arrays.asList(caseIds));
        Integer interfaceId = null;
        if (!CollectionUtils.isEmpty(testCaseList)) {
            interfaceId = testCaseList.get(0).getInterfaceId();
        }
        /**查询执行的接口**/
        Protocol protocol = interfaceMapper.getProtocolById(interfaceId);

        BuildTestInterfaceDo buildTestInterfaceDo = new BuildTestInterfaceDo();
        buildTestInterfaceDo.setBuildTestId(buildTestId);
        BeanUtils.copyProperties(protocol, buildTestInterfaceDo);
        buildTestInterfaceMapper.addBuildTestInterface(buildTestInterfaceDo);
        Integer buildTestInterfaceId = buildTestInterfaceDo.getId();
        this.addBuildTestCase(testCaseList, buildTestInterfaceId);
        return ResponseMessage.successResponse(buildTestId);
    }

    /**
     * 新增构建测试用例
     *
     * @param testCaseList
     * @param buildTestInterfaceId
     **/
    private void addBuildTestCase(List<TestCase> testCaseList, Integer buildTestInterfaceId) {
        List<BuildTestCaseDo> buildTestCaseDoList = Lists.newArrayList();
        testCaseList.stream().forEach(testCase -> {
            BuildTestCaseDo buildTestCaseDo = new BuildTestCaseDo();
            BeanUtils.copyProperties(testCase, buildTestCaseDo);
            buildTestCaseDo.setBuildTestInterfaceId(buildTestInterfaceId);
            buildTestCaseDoList.add(buildTestCaseDo);
        });
        if(!CollectionUtils.isEmpty(buildTestCaseDoList)){
            buildTestCaseMapper.addBuildTestCaseList(buildTestCaseDoList);
        }
    }
    /**
     * 获取当前项目的历史执行列表
     * **/
    public List<BuildTestDo> lastBuildTestByProjectId(Integer projectId){
        return buildTestMapper.lastBuildTestByProjectId(projectId);
    }

    public BuildTestDo buildRunTest(Integer buildTestId){
        return   buildTestMapper.buildTestById(buildTestId);
    }
    public BuildTestDo lastBuild(Integer projectId){
        return    buildTestMapper.lastBuild(projectId);
    }

    public List<BuildTestCaseDo> buildTestCaseDoList(Integer buildTestInterfaceId){
        BuildTestCaseDo buildTestCaseDo=new BuildTestCaseDo();
        buildTestCaseDo.setBuildTestInterfaceId(buildTestInterfaceId);
       return buildTestCaseMapper.findBuildTestCase(buildTestCaseDo);
    }
    /**
     * 构建测试计划,不保存在计划中,保存在构建列表
     *
     * @return list */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveBuildTestPlan(Integer testPlanId) {
        TestPlanDo testPlan = testPlanService.getTestPlanById(testPlanId);
        List<TestPlanDetailDo> testPlanDetailList = testPlan.getTestPlanDetailList();
        if (CollectionUtils.isEmpty(testPlanDetailList)) {
            return -1;
        }
        /**插入一个构建信息,状态、执行用户**/
        BuildTestDo buildTest = new BuildTestDo();
        Integer runMode = testPlan.getRunMode();
        buildTest.setRunMode(runMode);
        buildTest.setTestPlanId(testPlanId);
        buildTest.setTestPlanName(testPlan.getTestPlanName());
        buildTest.setProjectId(testPlan.getProjectId());
        buildTestMapper.addBuildTest(buildTest);
        Integer buildTestId = buildTest.getId();
        /**如果是场景执行则不插入**/
        if (runMode == 2) {
            return buildTestId;
        }
        /**通过接口的ids获取接口的信息**/
        for (TestPlanDetailDo testPlanDetailDo : testPlanDetailList) {
            Protocol protocol = interfaceMapper.getProtocolById(testPlanDetailDo.getInterfaceId());
            BuildTestInterfaceDo buildTestInterfaceDo = new BuildTestInterfaceDo();
            buildTestInterfaceDo.setBuildTestId(buildTestId);
            BeanUtils.copyProperties(protocol, buildTestInterfaceDo);
            buildTestInterfaceMapper.addBuildTestInterface(buildTestInterfaceDo);
            Integer buildTestInterfaceId = buildTestInterfaceDo.getId();
            List<Integer> ids = JSON.parseObject(testPlanDetailDo.getTestCaseIds(),List.class);
            List<TestCase> testCaseDoList = testCaseMapper.getTestCaseByIds(ids);
            /**增加构建用例**/
            if (!CollectionUtils.isEmpty(testCaseDoList)) {
                this.addBuildTestCase(testCaseDoList, buildTestInterfaceId);
            }
        }
        return buildTestId;
    }
}
